import time
import traceback

from absl import flags, app

from coin.exchange.upbit_v1.kr_rest.order_client import UpbitV1Order

flags.DEFINE_string("key_file", None, "Path of key_file which has authentication token.")

flags.DEFINE_integer("retry_count", 5, "")

FLAGS = flags.FLAGS


def main(_):
  for i in range(FLAGS.retry_count):
    try:
      print(UpbitV1Order('XRP', FLAGS.key_file).query_balance())
    except Exception as exc:
      traceback.print_exc()

      if i + 1 == FLAGS.retry_count:
        if (isinstance(exc, AssertionError) and len(exc.args) >= 1
            and exc.args[0]['error']['message'] == '알수없는 에러'):
          break
        else:
          raise
      else:
        time.sleep(5)
    else:
      break

  return 0


if __name__ == '__main__':
  app.run(main)
